import utils from "../common/util.js"

export default {
	
	// 根据id获取用户信息
	getInfoById(friendId){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl + "/chat/friend/info",
				method: "POST",
				data: {
					userId: uni.getStorageSync("userInfo").id,
					friendId: friendId
				},
				header:{
					"satoken":  uni.getStorageSync("userInfo").tokenValue
				},
				success: function(res) {
					if (res.data.code === 20000) {
						resolve(res.data.data)
					} else {
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: "网络错误！",
						showCancel: false
					});
				},
			})
		})
	},
	
	// 登录
	login(user){
		uni.request({
			url: utils.basicUrl+'/user/password',
			method: 'POST',
			data:{
				...user
			},
			success: function(res) {
				if(res.data.code === 20000){
					// 保存用户信息
					uni.setStorageSync("userInfo",res.data.data)
					
					// 保存用户列表信息
					var userList = uni.getStorageSync("userList")
					// debugger
					if(userList == ""){
						userList = []
						userList.push(res.data.data)
					}else{
						// 判断数组中是否包含该用户信息
						var flag = true
						for (var i = 0; i < userList.length; i++) {
							if(userList[i].id === res.data.data.id){
								// 更新 提到第一个
								flag = false
								userList.splice(i,1)
								userList.unshift(res.data.data)
							}
						}
						
						if(flag){
							userList.unshift(res.data.data)
						}
						uni.setStorageSync("userList",userList)
						
					}
					uni.setStorageSync("userList",userList)
					
					
					// 返回消息页面
					uni.reLaunch({
						url:'/pages/message/message',
						animationType: 'pop-in',
						animationDuration: 200
					})
					
				}else{
					utils.requestFail(res)
				}
			},
			fail: () => {
				uni.showModal({
					content: "网络错误！",
					showCancel: false
				});
			},
			complete() {
				// 停止当前页面下拉刷新
				// uni.hideLoading()
			}
		});
	},
	
	// 退出
	logout(){
		var user = uni.getStorageSync("userInfo");
		uni.request({
			url: utils.basicUrl+'/user/out',
			method: 'GET',
			header:{
				"satoken": user.tokenValue
			},
			success: function(res) {
				if(res.data.code === 20000){
					console.log(res.data)
					
					// 从登录列表删除该用户
					var userList = uni.getStorageSync("userList")
					if(userList.length == 0){
						uni.showToast({
							title:"退出成功",
							duration: 1000,
							icon:"none"
						})
						// 跳转登录页面
						uni.navigateTo({
							url:'../addAndRegister/addAndRegister',
							animationType: 'pop-in',
							animationDuration: 200
						})
					}
					for (var i = 0; i < userList.length; i++) {
						if(userList[i].id === user.id){
							// 删除
							userList.splice(i,1)
						}
					}
					this.userArray = userList
					uni.setStorageSync("userList",userList)
					// 判断当前用户列表是否为空
					if(userList.length == 0){
						uni.removeStorageSync("userInfo")
						uni.showToast({
							title:"已退出",
							duration: 1000,
							icon:"none"
						})
						// 跳转登录页面
						uni.navigateTo({
							url:'../addAndRegister/addAndRegister',
							animationType: 'pop-in',
							animationDuration: 200
						})
					}else{
						uni.showToast({
							title:"已切换用户",
							duration: 1000,
							icon:"none"
						})
						// 切换用户
						uni.setStorageSync("userInfo",userList[0])
					}
				}else{
					utils.requestFail(res)
				}
				
			},
			fail: () => {
				uni.showModal({
					content: "网络错误！",
					showCancel: false
				});
			},
			complete() {
				// 停止当前页面下拉刷新
				// uni.hideLoading()
			}
		});
	},
	// 根据关键字搜索好友
	search(key){
		return new Promise((resolve, reject) => {
			uni.request({
				url: utils.basicUrl + "/user/search",
				method: "GET",
				data: {
					key: key,
					userId: uni.getStorageSync("userInfo").id
				},
				header:{
					"satoken":  uni.getStorageSync("userInfo").tokenValue
				},
				success: function(res) {
					if (res.data.code === 20000) {
						var friendList = uni.getStorageSync(uni.getStorageSync("userInfo").id+"_friendList")
						if(friendList.length > 0 && res.data.data.length > 0){
							// 收集id
							var ids = []
							friendList.forEach(item => {
								ids.push(item.id)
							})
							
							// 判断当前用户是否已是好友
							// add：添加状态 0-可添加 1-添加中 2-已添加
							res.data.data.forEach(item => {
								if(ids.indexOf(item.id) !== -1){
									item.added = 2
								}else {
									item.added = 0
								}
							})
							
						}
						
						resolve(res.data.data)
						
					} else {
						utils.requestFail(res)
					}
				},
				fail: () => {
					uni.showModal({
						content: "网络错误！",
						showCancel: false
					});
				},
			})
		})
	}
	
}